package com.dream.utils.collection;

import java.util.Collection;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.ArrayUtils;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
/**
 * 
 * @author Evan
 * @date 2013/8/25
 *
 */
public class CollectionHelper {
	
	public static <T,E> Collection<E> arrToCol(T[] arr,Process<T,E> process){
		
		Collection<E> col = Lists.newArrayList();
		if(ArrayUtils.isNotEmpty(arr)){
			for(T item : arr){
				col.add(process.doProcess(item));
			}
		}
		
		return col;
	} 
	
	public static <T,E> Collection<E> colToCol(Collection<T> list,Process<T,E> process){
		
		Collection<E> col = Lists.newArrayList();
		if(CollectionUtils.isNotEmpty(list)){
			for(T item : list){
				col.add(process.doProcess(item));
			}
		}
		return col;
	} 
	
	public static <T,E> Collection<E> colToSet(Collection<T> list,Process<T,E> process){
		
		Collection<E> col = Sets.newHashSet();
		if(CollectionUtils.isNotEmpty(list)){
			for(T item : list){
				col.add(process.doProcess(item));
			}
		}
		return col;
	} 
	
	public static <T,E> List<E> colToList(Collection<T> list,Process<T,E> process){
		
		List<E> col = Lists.newArrayList();
		if(CollectionUtils.isNotEmpty(list)){
			for(T item : list){
				col.add(process.doProcess(item));
			}
		}
		return col;
	} 
	
	public static <T> String colToString(Collection<T> list,Process<T,String> process){
		
		StringBuilder builder = new StringBuilder();
		if(CollectionUtils.isNotEmpty(list)){
			int i=0,end = list.size()-1;
			for(T item : list){
				builder.append(process.doProcess(item));
				if(i < end){
					builder.append(",");
				}
				i++;
			}
		}
		return builder.toString();
		
	}
	
	public static interface Process<T,E>{
		public E doProcess(T t);
	}
}
